package com.power.sifa.correction.service.impl;

import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.service.Crc_personService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.correction.model.Crc_group_person;
import com.power.sifa.correction.dao.Crc_group_personDao;
import com.power.sifa.correction.service.Crc_group_personService;
import com.power.sifa.correction.model.pageInfo.Crc_group_personPageInfo;

import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 *  社区矫正分组内人员业务层实现
 */
@Service
public class Crc_group_personServiceImpl extends BaseServiceImpl implements Crc_group_personService {

    @Autowired
    private Crc_group_personDao dao;

    @Autowired
    private Crc_personService crc_personService;


    @Override
    public Crc_group_person getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Crc_group_person crc_group_person) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (crc_group_person.getPrimaryKey() != null) {
        	 Crc_group_person crc_group_personfDb = dao.findById(crc_group_person.getPrimaryKey()).orElse(null);
             if(crc_group_personfDb != null) {
                BeanUtils.copyProperties(crc_group_person, crc_group_personfDb);
                dao.save(crc_group_personfDb);
                return;
             }
        }
        dao.save(crc_group_person);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByGroupId(String groupId) {
        List<String> groupIds = new ArrayList<>();
        groupIds.add(groupId);
        dao.removeByGroupId(groupIds);
    }

    @Override
    public Page<Crc_group_person> findAll(Crc_group_personPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Crc_group_person> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Crc_group_person> list) {
		dao.saveAll(list);
	}


    @Override
    public void deleteByPersonId(String personId, String groupId) {
        dao.removeByPersonId(personId,groupId);
    }

    @Override
    public List<Crc_group_person> findGroupByPersonId(String personId) {
        List<String> personIds = new ArrayList<>();
        personIds.add(personId);
        return dao.findByPersonIds(personIds);
    }

    @Override
    public List<Crc_group_person> findGroupByPersonIds(List<String> personIds) {
        return dao.findByPersonIds(personIds);
    }

    @Override
    public List<Crc_group_person> findPersonByGroupId(String groupId) {
        List<String> groupIds = new ArrayList<>();
        groupIds.add(groupId);
        return dao.findByGroupIds(groupIds);
    }

    @Override
    public List<Crc_group_person> findPersonByGroupIds(List<String> groupIds) {
        return dao.findByGroupIds(groupIds);
    }
}
